package me.youline.dataServer.service.socialAction.handler;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import me.youline.dataServer.service.socialAction.event.FollowingSocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.FollowingSocialActionExeEvent;
import me.youline.dataServer.service.socialAction.event.PraiseSocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.PubMsgSocialActionExeEvent.PraiseSocialActionExeEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionEvent;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

/**
 * 该社交事件处理器可以根据事件类型指派给对应的处理器执行处理。
 * @author linhan
 *
 */
@Component
public class SocialActionEventHandlerComposite implements SocialActionEventHandler,BeanFactoryAware{

	private BeanFactory factory;
	
	private Map<Class< ? extends SocialActionEvent>,SocialActionEventHandler> handlers = new HashMap<Class< ? extends SocialActionEvent>,SocialActionEventHandler>();
	
	/**
	 * 定义了事件处理器与其事件类型的关系
	 */
	@PostConstruct
	private void init(){
		handlers.put(FollowingSocialActionExeEvent.class,factory.getBean(FollowingSocialActionEventHandler.class));
		handlers.put(PraiseSocialActionExeEvent.class,factory.getBean(PraiseSocialActionEventHandler.class));
		handlers.put(FollowingSocialActionCancelEvent.class,factory.getBean(FollowingSocialActionEventHandler.class));
		handlers.put(PraiseSocialActionCancelEvent.class,factory.getBean(PraiseSocialActionEventHandler.class));
	}
	
	@Override
	public void processSocialActionEvent(SocialActionEvent event) {
		handlers.get(event.getClass()).processSocialActionEvent(event);
	}

	@Override
	public void finishSocialActionEvent(SocialActionEvent event) {
		handlers.get(event.getClass()).finishSocialActionEvent(event);
		
	}

	@Override
	public boolean isSupport(SocialActionEvent event) {
		return handlers.get(event.getClass()) != null;//没有处理器对应当前事件则表示不支持
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.factory = beanFactory;
	}

	@Override
	public boolean isNeedHandle(SocialActionEvent event) {
		return handlers.get(event.getClass()).isNeedHandle(event);
	}

}
